//
//  ShareMyPinDanViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/17.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShareMyPinDanViewController.h"
#import "ShareData.h"
@interface ShareMyPinDanViewController ()
{
    enum WXScene _scene;
    NSTimer *_DrawTimer;
    NSInteger _remainSe;
    
}
@property(nonatomic,strong)ShareData *data;
@end

@implementation ShareMyPinDanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.senPinDan.layer.cornerRadius=4;
    self.senPinDan.clipsToBounds=YES;
    self.shareView.hidden=YES;
    self.bgView.hidden=YES;
     _remainSe=[_remaindSeconds integerValue];
    _DrawTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(updateTimeCountDown) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:_DrawTimer forMode:NSRunLoopCommonModes];
    
}
- (void)updateTimeCountDown
{
    _remainSe--;
    NSInteger interval = labs(_remainSe);
    NSInteger hours = ((NSInteger)interval)%(3600*24)/3600;
    NSInteger minutes = ((NSInteger)interval)%(3600*24)%3600/60;
    NSInteger seconds = ((NSInteger)interval)%(3600*24)%3600%60;
    if (_remainSe<=0)
    {
        [_DrawTimer fire];
        _DrawTimer=nil;
        [self.senPinDan setTitle:@"此拼单已失效" forState:UIControlStateNormal];
        self.senPinDan.backgroundColor=GRAYCOLOR;
        self.senPinDan.enabled=NO;
        self.hour1.text=@"0";
        self.hour2.text=@"0";
        self.minute1.text=@"0";
        self.minute2.text=@"0";
        self.second1.text=@"0";
        self.second2.text=@"0";
    }else
    {
        self.hour1.text=[NSString stringWithFormat:@"%ld",hours/10];
        self.hour2.text=[NSString stringWithFormat:@"%ld",hours%10];
        self.minute1.text=[NSString stringWithFormat:@"%ld",minutes/10];
        self.minute2.text=[NSString stringWithFormat:@"%ld",minutes%10];
        self.second1.text=[NSString stringWithFormat:@"%ld",seconds/10];
        self.second2.text=[NSString stringWithFormat:@"%ld",seconds%10];
    }
    
}
-(NSString *)interFormat:(NSInteger)number
{
    if (labs(number)<10)
    {
        return [NSString stringWithFormat:@"0%zd",number];
    }else
    {
        return [NSString stringWithFormat:@"%zd",number];
    }
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [GeneralTool showTabBar];
    [self shareViewDisappear];
    [_DrawTimer setFireDate:[NSDate distantFuture]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)shareMyPinDan:(id)sender {
   
    [self shareViewAppear];
}

//关闭分享页
- (IBAction)closeShareView:(id)sender {
    [self shareViewDisappear];
   
}
//分享到好友
- (IBAction)shareFriend:(id)sender {
    [self shareViewDisappear];
    [self getShareMaeesge];
    _scene=WXSceneSession;
    [self share];
}
//分享到朋友圈
- (IBAction)shareFriendComuinity:(id)sender {
    [self shareViewDisappear];
    [self getShareMaeesge];
    _scene=WXSceneTimeline;
    [self share];
}

-(void)shareViewDisappear
{
     self.bgView.hidden=YES;
    self.shareView.hidden=YES;
}
-(void)shareViewAppear
{
    self.bgView.hidden=NO;
    self.shareView.hidden=NO;
}
#pragma mark--获取要分享的内容
-(void)getShareMaeesge
{
    //http://192.168.1.107:3722/Common/PageInfo/{UserCode}/{PageType}/{ObjectID}/{Timestamp}/{Sign}
    NSString *timeStamp=[GeneralTool gettimestamp];
    // spellid 10 spellId要换回来
    NSDictionary *dic=@{@"UserCode":GETUSERCODE,
                        @"PageType":@"SHARE_URL_SPELL",
                        @"ObjectID":_spellID,
                        @"Timestamp":timeStamp};
    NSString *sign=[GeneralTool GetSin:dic];
    NSString *urlString=[NSString stringWithFormat:@"Common/PageInfo/%@/%@/%@/%@/%@",GETUSERCODE,@"SHARE_URL_SPELL",_spellID,timeStamp,sign];
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        if (error) {
            [MBProgressHUD showError:@"无法获取分享内容，请稍后再试"];
        }
        if([responses[@"IsSuccess"]integerValue]==1)
        {
            _data=[[ShareData alloc]initWithDic:responses[@"ReturnObject"]];
            
        }else
        {
            [MBProgressHUD showError:@"无法获取分享内容，请稍后再试"];
        }
        
    }];

}
#pragma mark--分享到微信
-(void)share
{
    WXWebpageObject *wxwebobj=[[WXWebpageObject alloc]init];
    wxwebobj.webpageUrl= _data.ShareUrl;
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = _data.ShareTitle;
    message.description=_data.ShareDes;
    NSURL *_url = [NSURL URLWithString: _data.ShareImg];// 获取的图片地址
    [message setThumbImage:[UIImage imageWithData: [NSData dataWithContentsOfURL:_url]]];
    message.mediaObject=wxwebobj;

    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.message = message;
    req.scene = _scene;
    [WXApi sendReq:req];

}

@end
